package com.zk.iotgateway.util;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.EventLoopGroup;
import io.netty.util.ReferenceCountUtil;
import org.springframework.util.StringUtils;

/**
 *
 * 工具类
 * @author:mn.m
 * @create 2019-04-17 下午 02:55
 **/
public class NettyUtil {


    /*
    * 获取端口
    * */
    public static String returnPort(ChannelHandlerContext ctx) {
            String tmp=ctx.channel().localAddress()+"";
            String serverPort=tmp.substring(tmp.lastIndexOf(":")+1);
            return serverPort;
    }


    /**
     * 关闭EventLoopGroup
     * @param group
     */
    public static void closeEventLoop(EventLoopGroup...  group ){
        for (EventLoopGroup eventLoopGroup : group) {
            eventLoopGroup.shutdownGracefully();
        }
    }


    /**
     *  释放buf
     * @param buf
     * */
    public static void releaseByteBuf(ByteBuf buf){
        if (buf!=null){
            ReferenceCountUtil.release(buf);
        }
    }


}
